package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.SysExtensionMapper;
import com.so206.po.SysExtension;
import com.so206.service.ExtensionService;
import com.so206.utils.BeanCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 返利 业务层
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ExtensionServiceImpl implements ExtensionService {

    @Autowired
    private SysExtensionMapper sysExtensionMapper;

    @Override
    public BigDecimal cout_exten_by_uid(Integer status, Long uid) {
        BigDecimal ret = new BigDecimal("0.00");
        List<SysExtension> list = sysExtensionMapper.selectByEidRidIdStatus(uid, null, null, status);
        if (list != null && list.size() > 0) {
            for (SysExtension sysExtension : list) {
                ret = ret.add(sysExtension.getMoney());
            }
        }
        return ret.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public Integer count_exten_by_uid(Integer status, Long uid) {
        List<SysExtension> list = sysExtensionMapper.selectByEidRidIdStatus(uid, null, null, status);
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public void save_exten(SysExtension extension) {
        extension.setCreateTime(new Date());
        sysExtensionMapper.insert(extension);
    }

    @Override
    public PageInfo<SysExtension> find_by_page(Integer page, Integer rows, Long e_uid, Long r_id, Long id, Integer status) {
        PageHelper.startPage(page, rows);
        List<SysExtension> list = sysExtensionMapper.selectByEidRidIdStatus(e_uid, r_id, id, status);
        return new PageInfo<>(list);
    }

    @Override
    public void delete(Long id) {
        sysExtensionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SysExtension find_by_id(Long id) {
        return sysExtensionMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(SysExtension extension) {
        SysExtension model = sysExtensionMapper.selectByPrimaryKey(extension.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(extension, model);
            sysExtensionMapper.updateByPrimaryKey(model);
        }
    }


}
